app.controller('ServerCtrl', ['$rootScope', '$scope', 'httpLoad', '$modal', '$stateParams','$timeout', '$state', 'CommonData','webSocket', function ($rootScope, $scope, httpLoad, $modal, $stateParams, $timeout,$state,CommonData,webSocket){
	$rootScope.link = '/statics/css/tpl-card.css';
	$scope.typeData = CommonData.hostType;
	$scope.param = {
		page: 1,
		rows: 10
	};
	$scope.showDetail = false;
	var resourcetabLocation = sessionStorage.getItem('resourcetabLocation');
	//详情机房列表
	if ($stateParams.id) {
		$rootScope.moduleTitle = '资源管理 > 机柜详情';
		$scope.param.params = angular.toJson([{param: {rackId: $stateParams.id}, sign: 'EQ'}])
	}
	//websocket异步操作
	webSocket.onmessage({
		message:function (data) {
			if($rootScope.currentUrl=='app.assets.resource'&&(data.operate=='platform.server')){
				$scope.getList();
			}
		}
	});
	//获取服务器列表数据
	$scope.getList = function (page) {
		$scope.selectAll1 = false;$scope.isBatch1 = true;
		$scope.param.page = page || $scope.param.page;
		httpLoad.loadData({
			url: '/server/list',
			data: $scope.param,
			noParam: true,
			success: function (data) {
				if (data.success) {
					$scope.serverListData = data.data.rows;
					$scope.totalCount = data.data.total;
					angular.forEach($scope.serverListData, function (data, index) {
						data.isSelect = false;
					});
					if(data.data.rows.length==0) $scope.isImageData = true;
					else $scope.isImageData = false;
				}else $scope.isImageData = true;
			}
		});
	};
	//如果Tab是当前页
	if(resourcetabLocation&&resourcetabLocation.indexOf("server")>=0) $scope.getList(1);
	//搜索
	$scope.search = function () {
		//对参数进行处理，去除空参数
		var toObjFormat = function (obj) {
			for (var a in obj) {
				if (obj[a] == "") delete obj[a];
			}
			return obj;
		}
		var params = [];
		var param1 = toObjFormat({
			name: $scope.name,
			managerIp:$scope.managerIp
		});
		var param2 = toObjFormat({
			type:$scope.type
		});
		if ($stateParams.id) {
			param2.rackId = $stateParams.id;
		};
		if (angular.toJson(param1).length > 2) params.push({param: param1, sign: 'LK'});
		if (angular.toJson(param2).length > 2) params.push({param: param2, sign: 'EQ'});
		$scope.param = {
			page: 1,
			rows: 10,
			params: angular.toJson(params)
		}
		$scope.getList();
	}
	//重置搜索条件
	$scope.reset = function () {
		var obj = ['name','tenantName','type','managerIp'];
		angular.forEach(obj, function (data) {
			$scope[data] = '';
		})
	};
	//批量操作
	$scope.isBatch1 = true;
	$scope.chooseAll1 = function(){
		$scope.isBatch1 = !$scope.selectAll1;
		$scope.serverListData.forEach(function(item){
			item.isSelect = $scope.selectAll1;
		});
	};
	$scope.choose1 = function(){
		var a = 0,b=0;
		$scope.serverListData.forEach(function(item){
			if(item.isSelect==true) a++;
			else b=1;
		});
		if(a>=1) $scope.isBatch1 = false;
		else $scope.isBatch1 = true;
		if(b==1) $scope.selectAll1 = false;
		else $scope.selectAll1 = true;
	};
	$scope.batch1 = function($event,value){
		$event.stopPropagation();
		$scope.operationData = '';var operationData = '';
		$scope.serverListData.forEach(function(item){
			if(item.isSelect==true) operationData += ','+item.id;
		});
		$scope.operationData = operationData.slice(1);
		switch (value){
			case 1:
			case 2:
				$scope.commendAction({'id':$scope.operationData,'batchUrl':1},value);
				break;
			case 3:
				$scope.delServer({"id":$scope.operationData,"batchUrl":1});
				break;
		}

	};
	//启动关闭
	$scope.commendAction = function (row,flag) {
		var tip,url,text;
		if(flag == 2){     //批量操作
			if(row.batchUrl&&row.batchUrl==1){
				tip = '确定要批量停止物理主机？';url = "/server/batchStop";text = '物理主机批量停止成功';
			}else{
				tip = '确定要停止物理主机？';url = "/server/stop";text = '物理主机停止成功';
			}
		}else{
			if(row.batchUrl&&row.batchUrl==1){
				tip = "确定要批量启动物理主机？";url="/server/batchStart";text = '物理主机批量启动成功';
			}else{
				tip = "确定要启动物理主机？";url="/server/start";text = '物理主机启动成功';
			}
		}
		var modalInstance = $modal.open({
			templateUrl: '/statics/tpl/assets/resource/delModal.html',
			controller: 'delModalCtrl',
			backdrop: 'static',
			resolve: {
				tip: function () {
					return tip;
				},
				btnList: function () {
					return [{name: '确定', type: 'btn-info'}, {name: '取消', type: 'btn-cancel'}];
				}
			}
		});
		modalInstance.result.then(function () {
			httpLoad.loadData({
				url: url,
				data: {
					id: row.id
				},
				success: function (data) {
					if (data.success) {
						if(row.batchUrl&&row.batchUrl==1){
							//批量操作的
							$scope.pop(data.message);
						}else{
							$scope.pop(text);
							if (flag == 1) {
								row.status = 'RUNNING';
							} else if(flag == 2) {
								row.status = 'STOPPED';
							}
						}
					}
				}
			});
			$scope.isBatch1 = true;$scope.selectAll1 = false;
			angular.forEach($scope.serverListData, function (data, index) {
				data.isSelect = false;
			});
		});
	};
	//分配
	$scope.allocate = function (id) {
		var modalInstance = $modal.open({
			templateUrl: '/statics/tpl/assets/infrastructure/allocateModal.html',
			controller: 'allocateModalCtrl',
			backdrop: 'static',
			// size:'lg'
		});
		modalInstance.result.then(function (data) {
			data.id = id;
			httpLoad.loadData({
				url: '/server/allot',
				data: data,
				success: function(data){
					if(data.success){
						$scope.pop('资源分配成功');
						$scope.getList();
					}
				}
			});
			$scope.isBatch1 = true;$scope.selectAll1 = false;
			angular.forEach($scope.serverListData, function (data, index) {
				data.isSelect = false;
			});
		});
	};
	//回收
	$scope.recover = function(id){
		var modalInstance = $modal.open({
			templateUrl: '/statics/tpl/operation/template/delModal.html',
			controller: 'delModalCtrl',
			backdrop: 'static',
			resolve:{
				tip: function () {
					return '你确定要回收该资源吗？';
				},
				btnList: function () {
					return  [{name:'回收',type:'btn-info'},{name:'取消',type:'btn-cancel'}];
				}
			}
		});
		modalInstance.result.then(function() {
			httpLoad.loadData({
				url: '/server/invoke',
				data: {id:id},
				success: function(data){
					if(data.success){
						$scope.pop('资源回收成功');
						$scope.getList();
					}
				}
			});
			$scope.isBatch1 = true;$scope.selectAll1 = false;
			angular.forEach($scope.serverListData, function (data, index) {
				data.isSelect = false;
			});
		});
	};
	//删除
	$scope.delServer = function (row) {
		var modalInstance = $modal.open({
			templateUrl: '/statics/tpl/assets/infrastructure/server/delModal.html',
			controller: 'delServerModalCtrl',
			resolve: {
				batchUrl: function () {   //批量操作才有
					return row.batchUrl;
				},
				id: function () {
					return row.id;
				}
			}
		});
		modalInstance.result.then(function () {
			$scope.getList();
			//$scope.rackDetail.servers -= 1;
		});
	};
	//跳转
	$scope.goAction = function (flag,id) {
		//if(flag == 1) id = $stateParams.id;
		$state.go('app.assets.addserver', {flag:flag+3,id: id})
	};
	//返回
	$scope.goBack = function(){
		$scope.isActive = false;
		$timeout(function() {
			$scope.showDetail = false;
		}, 200);
	};
	//四级联动加载数据
	$scope.loadSelectData = function (dcId,roomId,rackId) {//flag用来判断编辑还是选择，编辑时不清空id
		//获取数据中心列表数据
		httpLoad.loadData({
			url: '/dc/list',
			noParam: true,
			success: function (data) {
				if (data.success) {
					$scope.dataCenterData = data.data.rows;
					$scope.dataCenterData.forEach(function(item){
						if(item.id == dcId) $scope.itemData.dcName = item.name;
						return;
					});
				}
			}
		});
		httpLoad.loadData({
			url: '/room/list',
			noParam: true,
			success: function (data) {
				if (data.success) {
					$scope.roomData = data.data.rows;
					$scope.roomData.forEach(function(item){
						if(item.id == roomId) $scope.itemData.roomName = item.name;
						return;
					});
				}
			}
		});
		httpLoad.loadData({
			url: '/rack/list',
			noParam: true,
			success: function (data) {
				if (data.success) {
					$scope.cabinetData = data.data.rows;
					$scope.cabinetData.forEach(function(item){
						if(item.id == rackId) $scope.itemData.rackName = item.name;
						return;
					});
				}
			}
		});
	};
	//跳转详情页
	$scope.detail = function ($event,id) {
		$event.stopPropagation();
		//初始化默认值
		$scope.itemData = {
			type: 'PC',
			environment: 'DEVELOP'
		};
		var initDetailCreate = function () {
			$scope.disableSelect = true;
			httpLoad.loadData({
				url: '/rack/detail',
				method: 'GET',
				data: {id: id},
				success: function (data) {
					if (data.success) {
						var data = data.data;
						$scope.itemData.dcId = data.dcId;
						$scope.itemData.roomId = data.roomId;
						$scope.itemData.rackId = data.id;
						$scope.loadSelectData($scope.itemData.dcId,$scope.itemData.roomId,$scope.itemData.rackId);
					}
				}
			});
		};
		var initDetailEdit = function (id) {
			httpLoad.loadData({
				url: '/server/detail',
				method: 'GET',
				data: {id: id},
				success: function (data) {
					if (data.success) {
						$scope.itemData = data.data;
						$scope.showDetail = $scope.isActive = true;
						if ($scope.itemData.serviceStart) {
							$scope.date = $scope.itemData.serviceStart + ' - ' + $scope.itemData.serviceEnd;
						}
					}
				}
			});
		};
		$rootScope.moduleTitle = '资源管理 > 物理主机详情';
		$scope.modalName = '物理主机详情';
		$scope.isDetail = true;
		initDetailEdit(id);
	};
}]);
//删除
angular.module('app').controller('delServerModalCtrl', ['$scope', '$modalInstance', 'httpLoad', 'LANGUAGE', 'id','batchUrl',
	function ($scope, $modalInstance, httpLoad, LANGUAGE, id,batchUrl) {
		var text,url;
		if(batchUrl&&batchUrl==1){
			text = '物理主机批量删除成功';
			url = '/server/batchRemove';
			$scope.text = '确定要批量删除物理主机吗？';
		}else{
			text = '物理主机删除成功';
			url = '/server/remove';
			$scope.text = '确定要删除物理主机吗？';
		}
		$scope.ok = function () {
			httpLoad.loadData({
				url: url,
				data: {id: id},
				success: function (data) {
					if (data.success) {
						$scope.pop(text);
						$modalInstance.close();
					}
				}
			});
		};
		$scope.cancle = function () {
			$modalInstance.dismiss('cancel');
		};
	}
]);
//选择服务器ctrl
app.controller('allocateModalCtrl', ['$scope', 'httpLoad', '$modalInstance',
	function($scope, httpLoad, $modalInstance) {
		$scope.param = {
			page: 1,
			rows: 10
		}
		$scope.getList = function(page){
			$scope.param.page = page || $scope.param.page;
			$scope.isSelectAll = false;$scope.showDetail = false;
			httpLoad.loadData({
				url:'/tenant/list',
				method:'POST',
				noParam: true,
				data:$scope.param,
				success:function(data){
					if(data.success){
						$scope.listData = data.data.rows;
						$scope.totalPage = data.data.total;
					}
				}
			});
		}
		$scope.getList();
		//选择一个
		$scope.selectItem = function ($event,row) {
			$event.stopPropagation();
			$event.preventDefault();
			if(row.isSelected){
				row.isSelected = !row.isSelected;
			}else{
				$scope.listData.forEach(function (item) {
					item.isSelected = false;
				});
				row.isSelected = !row.isSelected;
			}
		}
		//对参数进行处理，去除空参数
		var toObjFormat = function(obj) {
			for (var a in obj) {
				if (obj[a] == "") delete obj[a];
			}
			return obj;
		};
		//搜索
		$scope.search = function(){
			var params = [];
			var param1 = toObjFormat({
				name:$scope.name,
			});
			if (angular.toJson(param1).length > 2) params.push({param: param1, sign: 'LK'});
			$scope.param = {
				page: 1,
				rows: 10,
				params: angular.toJson(params)
			}
			$scope.getList(1)
		}
		var toFormatTime = function(time, place) {
			if (!time) return "";
			var date = time.split(' - ');
			return date[place/1];
		}
		$scope.ok = function () {
			$scope.listData.forEach(function (item) {
				if(item.isSelected){
					if(!item.date) return $scope.pop('请选择服务时间','error');
					var obj = {
						id:item.id,
						startTime:toFormatTime(item.date, 0),
						endTime:toFormatTime($scope.date, 1)
					};
					$modalInstance.close(obj);
				}
			});
		}
		$scope.cancle = function () {
			$modalInstance.dismiss('cancel');
		};
	}
])